/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package PropertyRental.war.servlets;

import PropertyRental.ejb.persistence.Property;
import PropertyRental.war.beans.Catalog;
import PropertyRental.war.beans.CatalogProperty;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Sébastien
 */
public class SearchPropertyAction extends Action {
    public SearchPropertyAction(String success, String fail) {
        super(success,fail);
    }

    @Override
    public String perform(HttpServletRequest req) {

        String type = req.getParameter("type");

        int numberBedroom;
        if(req.getParameter("numberBedroom").equals(""))
            numberBedroom = -1;
        else
            numberBedroom = Integer.parseInt(req.getParameter("numberBedroom"));

        int numberBathroom;
        if(req.getParameter("numberBathroom").equals(""))        
            numberBathroom = -1;
        else
            numberBathroom = Integer.parseInt(req.getParameter("numberBathroom"));
        
        int numberOtherRoom;
        if(req.getParameter("numberOtherRoom").equals(""))
            numberOtherRoom = -1;
        else
            numberOtherRoom = Integer.parseInt(req.getParameter("numberOtherRoom"));

        double minrent;
        if(req.getParameter("minrent").equals(""))
            minrent = -1;
        else
            minrent = Double.parseDouble(req.getParameter("minrent"));

        double maxrent;
        if(req.getParameter("maxrent").equals(""))
            maxrent = -1;
        else
            maxrent = Double.parseDouble(req.getParameter("maxrent"));
        
        String location = req.getParameter("location");

        ArrayList<CatalogProperty> results = new ArrayList<CatalogProperty>();
        List<Property> resultList = propertyEJB.findUsingCriteria(type, numberBedroom,
                numberBathroom, numberOtherRoom, minrent, maxrent, location);

        if (resultList != null) {
            for (int i=0; i<resultList.size(); i++) {
                    Property x=(Property)resultList.get(i);
                    results.add(new CatalogProperty(x.getId(),x.getType(), x.getAdress(), x.getNumberBedroom(),
                            x.getNumberBathroom(), x.getNumberOtherRoom(), x.getRent(), x.getLocation()));
            }
        }
        req.setAttribute("title", "Seach Properties by Criteria");
        Catalog properties = new Catalog();
        properties.setProperties(results);
        req.setAttribute("catalog", properties);

        return this.getSuccesspage();


    }

}
